<template>
  <div>
    <!-- 搜索 -->
    <!-- 索赔结算单维护 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="110" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :onColumnsChange="(columns) => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :is-select-column="false"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="text" style="font-size: 14px; color: #bb2801; font-weight: bold">
<!--          <span >每月{{start}}-{{end}}日可维护此界面</span>-->
          <span >如需维护结算单，请联系管理员开放</span>
        </el-button>
        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()" :disabled="flag">新增</multiuse-button>
        <multiuse-button size="small" type="primary" @click="exportFileBtn()">导出</multiuse-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="visible" :title="'结算单编辑'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <xiangqing @close="close" @save="save" :rowData="rowData"></xiangqing>
    </Drawer>
    <Drawer :visible.sync="visibleAdd" :title="'结算单新增'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AddForm @close="closeAdd" @save="saveAdd"></AddForm>
    </Drawer>
    <Drawer :visible.sync="chakanFlag" :title="'结算单详情'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <showDetail @close="chakanclose" @save="chakanAdd" :rowData="rowData"></showDetail>
    </Drawer>
  </div>
</template>
<script>
import xiangqing from './xiangqing.vue';
import AddForm from './AddForm.vue';
import showDetail from './showDetail.vue';
import { exportExcelFile } from '@/utils/index';
import { getListByPage, getPartType, quyrPSettle, deleteOne, editSave, getListForExport, submitById,exporturl } from '@/api/claim/dealer/ClaimStatementMaintain';
import { selectByParaCode, multiples } from '@/api/claim/factory/SystemParameterMaintain';
import { getAppId } from '@/assets/js/auth';
import { dictionary } from '@/utils/dictMixin';
import moment from "moment";
export default {
  mixins: [dictionary],
  components: {
    xiangqing,
    AddForm,
    showDetail,
  },
  data() {
    return {
      dataList: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      chakanFlag: false,
      infoId: null,
      editRow: {},
      rowData: {},
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      fetchapi: getListByPage,
      columns: this.creatHeader(),
      fetchParams: {xhrAbort: true}, //ipa 接口
      selectedRows: [], //选中经销商
      visibleAdd: false,
      flag:false,
      start:null,
      end:null
    };
  },
  async created() {
    const res = await selectByParaCode({ paraCode: 'XTCS00014'});
    var date = new Date();
    var day = date.getDate();
    this.start = res.data.para1;
    this.end = res.data.para2;
    if(day<res.data.para1 ||day>res.data.para2){
      this.flag = true;
    }else{
      this.flag = false;
    }
    if (getAppId() == 'arcfox') {
      this.topFilterInit.find(x => x.fieldName == 'carType').itemList =this.createDictList('5543',[55431001])
    }else{
      this.topFilterInit.find(x => x.fieldName == 'carType').itemList =this.createDictList('5543')
    }
    this.fetchParams.beginSettleDate = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.endSettleDate = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.chuqi();
  },
  methods: {
    // 导出
   async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000205' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginCreatedAt = this.topFilterInit.find(x => x.fieldName == "beginSettleDate|endSettleDate").initialValue[0];
      // let endCreatedAt = this.topFilterInit.find(x => x.fieldName == "beginSettleDate|endSettleDate").initialValue[1];
      // if(!beginCreatedAt) {
      //   this.$notify.warning({ title: '提示信息', message: '创建开始日期不可为空！' });
      //   return
      // }
      // if(!endCreatedAt) {
      //   this.$notify.warning({ title: '提示信息', message: '创建结束日期不可为空！' });
      //   return
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(endCreatedAt).format('YYYY-MM-DD')).diff(moment(beginCreatedAt), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(endCreatedAt).format('YYYY-MM-DD')).diff(moment(beginCreatedAt), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      // let obj = Object.assign({}, this.fetchParams, {xhrAbort: false});
      // obj.beginSettleDate = this.topFilterInit.find(x => x.fieldName == "beginSettleDate|endSettleDate").initialValue[0];
      // obj.endSettleDate = this.topFilterInit.find(x => x.fieldName == "beginSettleDate|endSettleDate").initialValue[1];
      // getListForExport(obj).then((res) => {
      //   exportExcelFile(res.data, '索赔结算单信息');
      // });
    },
    // 删除
    async delFun(val) {
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          this.res = await deleteOne(val.settleId);
          if (this.res.resultCode != 200) {
            this.$notify.error({ title: '提示信息', message: res.errMsg || '删除失败!' });
          } else {
            this.$notify.success({ title: '提示信息', message: '删除成功!' });
            this.fetchParams = { ...this.fetchParams };
          }
        })
    },
    //翻译
    async chuqi() {
      // const res = await getPartType({ type: 5546 });
      // if (res.resultCode == 200) {
      //   this.topFilterInit.find((x) => x.fieldName == 'settleStatus').initialValue = '';
      //   this.topFilterInit.find((x) => x.fieldName == 'settleStatus').itemList = res.data.map((y) => ({ text: y.codeCnDesc, value: y.codeId }));
      // }
      // const res4 = await getPartType( {type:5543});
      // if (res4.resultCode == 200) {
      //
      //   if (getAppId() == 'arcfox') {
      //     delete res4.data[0];
      //   }
      //
      //   this.topFilterInit.find(x => x.fieldName == 'carType').initialValue = '';
      //   this.topFilterInit.find(x => x.fieldName == 'carType').itemList = res4.data.map(y =>({ text: y.codeCnDesc, value: y.codeId }));
      // }
    }, // 提交
    async submitFun(val) {
      this.$confirm('确定提交吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          let res1 = await submitById(val.settleId);
          // this.editRow = res1.data;
          // //状态改成提交
          // this.editRow.settleStatus = 55461002;
          // let res = await editSave(this.editRow);
          if (res1.resultCode != 200) {
            this.$notify.error({ title: '提示信息', message: res.errMsg || '提交失败!' });
          } else {
            this.$notify.success({ title: '提示信息', message: '提交成功!' });
            this.fetchParams = { ...this.fetchParams };
          }
        })
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    close() {
      this.visible = false;
    },
    chooseOrderNo(val) {
      this.rowData = val;
      this.visible = true;
    },
    chakan(val) {
      this.rowData = val;
      this.chakanFlag = true;
    },
    save() {
      this.visible = false;
      this.fetchParams = { ...this.fetchParams };
    },
    add() {
      this.visibleAdd = true;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    chakanclose() {
      this.chakanFlag = false;
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    chakanAdd() {
      this.chakanFlag = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //搜索获取数据
    changeHandle(val) {
      if(!val.beginSettleDate) {
        this.$notify.warning({ title: '提示信息', message: '创建开始日期不可为空！' });
        return
      }
      if(!val.endSettleDate) {
        this.$notify.warning({ title: '提示信息', message: '创建结束日期不可为空！' });
        return
      }
      if(getAppId() == 'arcfox') {
        if (moment(moment(val.endSettleDate).format('YYYY-MM-DD')).diff(moment(val.beginSettleDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
          return
        }
      } else {
        if (moment(moment(val.endSettleDate).format('YYYY-MM-DD')).diff(moment(val.beginSettleDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
          return
        }
      }
      this.fetchParams = Object.assign({}, this.fetchParams, val,{xhrAbort: false});
      console.log(val);
    },
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '结算单号', fieldName: 'settleNo', placeholder: '请输入结算单号...', initialValue: '' },
        { type: 'SELECT', label: '结算单状态', fieldName: 'settleStatus', placeholder: '请选择结算单状态...', initialValue: '', itemList: this.createDictList('5546') },
        {
          type: 'RANGE_DATE',
          label: '创建时间',
          fieldName: 'beginSettleDate|endSettleDate',
          placeholder: '请输入',
          initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ],
          rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
        { type: 'SELECT', label: '车辆属性', fieldName: 'carType', placeholder: '请选择车辆属性...', initialValue: '',itemList: [] }
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          fixed: 'left',
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 160,
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" disabled={props.row.settleStatus == '55461002' || props.row.settleStatus == '55461003' ||this.flag ? true : false} onClick={() => this.delFun(props.row)}>
                  删除
                </multiuse-button>
                <multiuse-button
                  size="mini"
                  type="text"
                  disabled={props.row.settleStatus == '55461002' || props.row.settleStatus == '55461003' || this.flag ? true : false}
                  onClick={() => this.submitFun(props.row)}
                >
                  提交
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.chakan(props.row)}>
                  查看
                </multiuse-button>
              </div>
            );
          },
        },
        { title: '结算单状态', dataIndex: 'settleStatusStr', sorter: true },
        { title: '结算单号', dataIndex: 'settleNo', sorter: true },
        { title: '车辆属性', dataIndex: 'carTypeStr', sorter: true },
        { title: '索赔单数', dataIndex: 'claimCount', sorter: true },
        { title: '结算金额', dataIndex: 'settleAmount', sorter: true },
        { title: '考核金额', dataIndex: 'assessAmount', sorter: true },
        { title: '提交时间', dataIndex: 'applyDate', sorter: true },
        // { title: '主键', dataIndex: 'settleId', hidden: true },
      ];
    },

  },
  // mounted() {
  //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
  // }
};
</script>

<style lang="less" scoped></style>
